//
//  Person.swift
//  Person
//
//  Created by 左忠飞 on 2021/8/9.
//

import Foundation

class Person: CustomStringConvertible {
    let name: String
    var assets = [Asset]()
    let accountant = Accountant()
    var description: String {
        return "Persion(\(name)"
    }

    init(name: String) {
        self.name = name
        
        accountant.netWorthChangedHandler = {
            [weak self] netWorth in
            self?.netWorthDidChange(to: netWorth)
            return
        }
    }

    func takeOwnership(of asset: Asset) {
//        asset.owner = self
//        assets.append(asset)
        accountant.gained(asset) {
            asset.owner = self
            assets.append(asset)
        }
    }

    func netWorthDidChange(to netWorth:Double){
        print("The net Worth of \(self) is now \(netWorth)")
    }
    
    deinit {
        print("\(self) is being deallocated")
    }
}
